package com.qd.extcam;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.SurfaceHolder;

 
public class ExtRecorder extends MediaRecorder{
	private Camera camera;
	private SurfaceHolder sh;
	private Thread t;
	private boolean running;
	private String sensorFileName;
	private String mediaFileName;

	public ExtRecorder(SurfaceHolder sh){
		this.sh = sh;
		t = new Thread();
	}
	
	public void InitMediaRecorder(){
        // Step 1: Unlock and set camera to MediaRecorder
		camera = Camera.open();
        camera.unlock();
        //camera.setDisplayOrientation(180);
        super.setCamera(camera);
        //super.setOnInfoListener(m_listener);
        super.setMaxFileSize(Long.MAX_VALUE);
       
        super.setAudioSource(MediaRecorder.AudioSource.MIC);
        super.setVideoSource(MediaRecorder.VideoSource.CAMERA);
 
        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
        //super.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        
 
        super.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        super.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        super.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        super.setVideoSize(640,480);
        super.setVideoFrameRate(30);
        
        /*
         * 16K - 화상전화
         * 128K ~ 384K - 화상회의
         * 9200K DVD
         * 13000K ~ 19000K HD
         */
        super.setVideoEncodingBitRate(1920*1000);
        
        
        mediaFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +Long.toString(System.currentTimeMillis()) + ".mp4";
        super.setOutputFile(mediaFileName);
        super.setPreviewDisplay(sh.getSurface());
	}
	
	public void setSensorFile(String fileName){
		sensorFileName = fileName;
	}
	
	public void prepare() {
		try {
			super.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void start() {
		super.start();
		running = true;
		/*(t = new Thread() {
			public void run() {
				while(running){
					
				}
			}
		}).start();*/ 
	}
	
	public void stop() {
		super.stop();
		this.camera.release();
		super.release();
		running = false;	
		mergeFile();
	}
	
	private void mergeFile(){
		File mFile = new File(mediaFileName);
		File sFile = new File(sensorFileName);
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			fos = new FileOutputStream(mFile, true);
			fis = new FileInputStream(sFile);
			if (fos != null && fis != null) {
				long mLength = mFile.length();
				byte[] buffer = new byte[1024];
				int readLength = 0;
				while((readLength = fis.read(buffer)) > 0 ){
					fos.write(buffer,0, readLength);
				}
				String sTag = "SENSOR_VAL";
				fos.write(sTag.getBytes());
				fos.write(Long.toHexString(mLength).getBytes());
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null)
					fos.close();	
				if(fis != null)
					fis.close();
				if(sFile != null)
					sFile.delete();
			} catch(Exception e) {
				e.printStackTrace();
			}
		}

	}
	
}
